﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using zijian666.AnyExtensions;

namespace zijian666.ComponentModel.DataAnnotations;

/// <summary>
/// 不允许指定的值
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class NotAttribute : ValidationAttribute
{
	private readonly object[] _values;

	public NotAttribute(params object[] values)
	{
		values = values.WithoutNull().ToArray();
		if (values.Length == 0)
		{
			throw new ArgumentNullException(nameof(values));
		}
		_values = values;
        ErrorMessage = "字段 {0} 不能是 " + _values.Join(",");
	}

	public override bool IsValid(object value)
    {
        if (value is null)
        {
            return true;
        }
        foreach (var val in _values)
		{
			if (val.Equals(value))
			{
				return false;
			}
		}
		return true;
	}
}
